/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.core.sql.result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by walle on 12/9/15.
 */
public class DataResults {
    private static final Logger log = LoggerFactory.getLogger(DataResults.class);

    public static DataResult setException(DataResult result, Exception e) {
        if (result == null) {
            result = DataResult.getInstance(new ArrayList(), 0, 1, 10);
        }

        if (e != null) {
            result.setMessage(e + e.getMessage());
            log.error(result.getMessage());
        }
        return result;
    }

    public static List<Map<String, Object>> getMaps(String names, List<Object[]> t) {
        if ((names == null) || (t == null)) {
            throw new IllegalArgumentException("参数异常！");
        }
        names = names.replace("，", ",").toUpperCase();
        String[] nameArray = names.split(",");
        List list = new ArrayList();
        for (Iterator iterator = t.iterator(); iterator.hasNext(); ) {
            Object[] objects = (Object[]) iterator.next();
            Map mapresult = new HashMap();

            int valueLength = objects.length;
            if (nameArray.length < valueLength)
                valueLength = nameArray.length;
            try {
                for (int i = 0; i < valueLength; i++) {
                    Object value = objects[i];
                    String key = null;
                    if (names.trim().equals("*"))
                        key = String.valueOf(i);
                    else {
                        try {
                            key = nameArray[i].trim();
                            if (key.indexOf(" AS ") >= 0) {
                                key = key.substring(key.trim().indexOf(" AS ") + 3);
                            }

                            key = key.replace(".", "_").trim();
                        } catch (Exception e) {
                            log.error("参数填充错误！（key和value数量可能不匹配）");
                        }
                    }
                    mapresult.put(key, value);
                }
            } catch (Exception e) {
                log.error("参数填充错误！（key和value数量可能不匹配）");
            }
            list.add(mapresult);
        }
        return list;
    }

    public static List<String> getTitles(String names) {
        List list = new ArrayList();
        if (names == null) {
            throw new IllegalArgumentException("参数异常！");
        }
        names = names.replace("，", ",").toUpperCase();
        String[] nameArray = names.split(",");
        for (String name : nameArray) {
            String key = "";
            key = name.trim();
            if (key.indexOf(" AS ") >= 0) {
                key = key.substring(key.trim().indexOf(" AS ") + 3);
            }
            key = key.replace(".", "_").trim();
            list.add(key);
        }
        return list;
    }

    public static String getMapKey(String key) {
        key = key.trim();
        if (key.indexOf(" AS ") >= 0) {
            key = key.substring(key.trim().indexOf(" AS ") + 3);
        }
        key = key.replace(".", "_").trim();
        return key.toUpperCase();
    }

    public static void printMaps(List<Map<String, Object>> listmap) {
        Iterator iterator = listmap.iterator();
        while (iterator.hasNext()) {
            Map name = (Map) iterator.next();
            Set keyset = name.keySet();
            Iterator iterator2 = keyset.iterator();
            while (iterator2.hasNext()) {
                String name2 = (String) iterator2.next();
                System.out.println(name2 + ":" + name.get(name2));
            }
            System.out.println("------------------------------------------");
        }
    }
}
